""" My
SQL数据库模型
@Project: Rebar
@File: user_model.py
@Author: hzc
@Date: 2023/3/21 23:28
"""
import re
from enum import Enum
from sqlalchemy import Column, Integer, String, DateTime, Text, Float, func, SmallInteger, UniqueConstraint, \
    Enum as SqlEnum, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import validates, relationship
from model.params import RoleNameEnum

Base = declarative_base()


class TbAdmin(Base):
    __table_args__ = {
        'schema': 'reservoir'
    }
    __tablename__ = 'tb_admin'
    user_id = Column(Integer, primary_key=True, comment='用户ID')
    user_name = Column(String(16), comment="用户名", nullable=False)
    user_pwd = Column(String(500), comment="密码", nullable=False)
    role_name = Column(SqlEnum(RoleNameEnum), comment="角色名", nullable=False)


class TbUsers(Base):
    # 指明本表为reservoir库中
    __table_args__ = {
        'schema': 'reservoir'
    }
    __tablename__ = 'tb_users'
    user_id = Column(Integer, primary_key=True, comment='用户ID')
    user_name = Column(String(16), unique=True, comment="用户名", nullable=False)
    user_pwd = Column(String(500), comment="密码", nullable=False)
    #  这个怎么回事
    role_name = Column(SqlEnum(RoleNameEnum), comment="角色名", nullable=False)
    company_name = Column(String(20),comment='所在公司')
    belong_post = Column(String(50),comment='所属岗位')
    job_name = Column(String(50), comment='所在岗位')
    true_name = Column(String(50), comment='真实姓名')
    phone = Column(String(20), default=None, comment='手机号')
    email = Column(String(30), unique=True, comment='邮箱')
    is_authorize = Column(Integer, default=0, comment='用户是否被授权', nullable=False)
    group_id = Column(Integer,comment='权限分组', default=1)
    check_date = Column(DateTime, default=None, comment='审核时间')
    check_name = Column(String(30), default=None, comment='审核人')
    created_time = Column(DateTime, comment='创建时间', default=func.now())
    updated_time = Column(DateTime, comment='修改时间', default=func.now(), onupdate=func.now())
    remark = Column(Text, comment='备注')

    # @validates('phone')
    # def validate_phone(self, key, value):
    #     """
    #     当添加手机号字段时会调用本方法
    #     :param key:
    #     :param value:
    #     :return:
    #     """
    #     if value != "" and not re.match(r'^1[3-9]\d{9}$', value):
    #         raise ValueError('手机号格式有误')
    #     return value
    #
    # @validates('email')
    # def validate_email(self, key, value):
    #     """
    #     当添加邮箱字段时会调用本方法
    #     re:
    #     *: 0个及以上字符， 相当于{0,}
    #     +: 一个及以上字符，相当于{1,}
    #     [a-zA-Z0-9]: 里面的任意字符
    #     :param key:
    #     :param value:
    #     :return:
    #     """
    #     if value != "" and not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    #             , value):
    #         raise ValueError('邮箱格式有误')
    #     return value


class Tb_Depart(Base):
    __table_args__ = {
        'schema': 'reservoir'
    }
    # 职位表
    __tablename__ = 'tb_depart'
    depart_id = Column(Integer, primary_key=True, comment='部门ID')
    depart_name = Column(String(50), unique=True, comment="部门名称", nullable=False)
    #   Text  和smallinterger
    Remark = Column(Text, default="", comment='备注')
    company_id = Column(Integer, comment='所属公司', nullable=False)


class TbCompanies(Base):
    __table_args__ = {
        'schema': 'reservoir'
    }
    # 建筑公司信息
    __tablename__ = 'tb_companies'
    company_id = Column(Integer, primary_key=True, comment='公司ID')
    company_name = Column(String(50), unique=True, comment='公司名称', nullable=False)
    company_code = Column(String(50), unique=True, comment='机构代码', )
    company_representative = Column(String(10), comment='法人代表')
    auth_number = Column(String(20), default="", comment='授权序列号')
    is_authorized = Column(Integer, default=0, comment='授权状态')
    authorized_person = Column(String(10), default="", comment='授权人')

    # default只有创建的时候会执行，onupdate在每次修改时都会执行
    authorized_startdate = Column(DateTime, comment='授权日期', default=func.now())
    authorized_enddate = Column(DateTime, comment='授权截止日期', default=func.now())
    authorized_remak = Column(Text, default="无授权备注", comment='备注')
    authorized_online_number = Column(Integer, default=0, comment='限定在线数')
    user_db_constr = Column(String(200), default="", comment='用户库连接串')
    remark = Column(Text, default="无备注", comment='备注')


class TbProject(Base):
    __table_args__ = {
        'schema': 'reservoir'
    }
    # 项目信息表
    __tablename__ = 'tb_project'
    project_id = Column(Integer, primary_key=True,autoincrement=True, comment='项目ID')
    project_name = Column(String(50), unique=True, comment='项目名称')
    create_user = Column(Integer, primary_key=True, comment='创建人')
    is_share = Column(Integer, default=0, comment='是否共享')
    created_time = Column(DateTime, comment='创建时间', default=func.now())
    updated_time = Column(DateTime, comment='修改时间', default=func.now(), onupdate=func.now())
    remark = Column(String(200), default="", comment='项目说明')


# class Group(Base):
#     __table_args__ = {
#         'schema': 'reservoir'
#     }
#     # 权限组
#     __tablename__ = 'tb_auth_group'
#     id = Column(Integer, primary_key=True, index=True)
#     name = Column(String(150), unique=True, index=True, comment="组名")
#
#     def __str__(self):
#         return self.name
#
#
# class Permission(Base):
#     __table_args__ = {
#         'schema': 'reservoir'
#     }
#     # 权限表
#     __tablename__ = 'tb_auth_permission'
#     name = Column(String(128), unique=True, index=True, comment="权限名称")  # 权限名称
#     codename = Column(String(100), comment="权限字段", primary_key=True)  # 权限字段,也是我们平判断权限输入的字段
#
#     def __str__(self):
#         return self.name
#
# class auth_group_permission(Base):
#     __table_args__ = {
#         'schema': 'reservoir'
#     }
#     # 权限组的权限
#     __tablename__ = 'auth_group_permission'
#     id = Column(Integer, primary_key=True, index=True)
#     group_id = Column(Integer, ForeignKey("tb_auth_group.id"))
#     codename = Column(String(100), ForeignKey("tb_auth_permission.codename"))





class TbLog(Base):
    __table_args__ = {
        'schema': 'reservoir'
    }
    """日志记录表"""
    __tablename__ = "tb_log"
    id = Column(Integer, primary_key=True, autoincrement=True, comment="id")
    client = Column(String(20), comment="请求ip")
    method = Column(String(10), comment="请求方式")
    path = Column(String(50), comment="请求路径")
    query_params = Column(String(50), comment="查询参数")
    path_params = Column(String(50), comment="路径请求参数")
    error_info = Column(Text, comment="错误信息")
    created_time = Column(DateTime, comment='创建时间', default=func.now())
    updated_time = Column(DateTime, comment='修改时间', default=func.now(), onupdate=func.now())
